<?php

namespace app\common\util\jsonscm;
use app\common\exception\CatchException;
use Opis\JsonSchema\Errors\ErrorFormatter;
use Opis\JsonSchema\Validator;

abstract class JsonScmValidator
{
    protected static $schema = '';
    /**
     * 检验相册json
     *
     * @param mixed $data
     * 
     * @return array|true true时为校验通过，否则返回错误信息数组
     */
    public static function validate(mixed $data): array|bool
    {
        $validator = new Validator();
        $result = $validator->validate($data, self::$schema);
        if ($result->isValid()) {
            return true;
        } else {
            return (new ErrorFormatter())->formatOutput($result->error(), 'detailed');
        }
    }

    /**
     * 校验并在错误时抛出CatchException异常
     *
     * @param mixed $data 数据
     * @throws CatchException
     */
    public static function tpValidate($data)
    {
        $result = static::validate($data);
        dump($result);
        if ($result === true) {
            return true;
        } else {
            return json_encode($result);
        }
    }
}